/**
 * Created by zyy on 15/9/8.
 * zhangyuyu@superjia.com
 */
(function ($) {
    'use strict';

    var template = {

        __collection: new (Backbone.Collection.extend({
            model: Backbone.Model,
            parse: function (response, xhr) {
                if (response.code != 0) {
                    return [];
                }
                var items = response.data;
                if (!items) return response;
                _.each(items, function (item) {
                    
                });

                return items;
            }
        })),

        api: function(key){
            var rootPath = '/api/project';
            return rootPath + {
                list: '',
                create: '/create'
            }[key];
        },

        init: function (container, options) {
            var self = this;
            self.__container = container;
            self.__options = options;

            self.__collection.bind('reset', self.addItems, self)
            self.__collection.bind('add', self.addItem, self)

            self.__events();
        },

        __events: function () {
            var self = this;
            var container = self.__container;

            container.on('click', '.demo', function () {

            });
        }

        addItems: function (collection, options) {
            var self = this
            collection.each(function (model) {
                self.addItem(model, collection)
            })
        },

        addItem: function (model, collection, options) {
            var self = this
            var view = new App.modules.templateView({
                model: model
            })
            self.__container.append(view.render().$el);
        }
    };

    App.modules.template = template;
})(jQuery);